package com.milhouz.findmydroid.view;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.milhouz.findmydroid.CustomCharsActivity;
import com.milhouz.findmydroid.R;
import com.milhouz.findmydroid.bean.Character;
import com.milhouz.findmydroid.bean.Game;
import com.milhouz.findmydroid.db.AppDbAPI;

public class MenuItemAdapter extends ArrayAdapter<MenuItem> {

	private final Activity mParentActivity;
	private final Game mGame;
	private MenuItem[] items;

	public MenuItemAdapter(Activity parentActivity, Game game, MenuItem[] items) {
		super(parentActivity.getApplicationContext(), R.layout.menu_item, items);
		this.mParentActivity = parentActivity;
		this.mGame = game;
		this.items = items;
	}

	public void updateItems(MenuItem[] items) {
		this.items = items;
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.menu_item, null);
		}

		TextView label = (TextView) v.findViewById(R.id.label);
		TextView desc = (TextView) v.findViewById(R.id.desc);
		TextView additionalText = (TextView) v.findViewById(R.id.additionalText);
		ImageView imgLeft = (ImageView) v.findViewById(R.id.menuImageLeft);

		MenuItem menuItem = items[position];
		if (menuItem != null) {
			switch (menuItem.getType()) {
			case TEAMS:
				label.setText(mParentActivity.getString(R.string.menu_nbteams_title));
				desc.setText(mParentActivity.getString(R.string.menu_nbteams_summary));
				imgLeft.setImageResource(R.drawable.ic_menu_users);
				additionalText.setText(String.valueOf(mGame.getNbTeams()));
				break;
			case TIME:
				label.setText(mParentActivity.getString(R.string.menu_timer_title));
				desc.setText(mParentActivity.getString(R.string.menu_timer_summary));
				imgLeft.setImageResource(R.drawable.ic_menu_alarm);
				additionalText.setText(String.valueOf(mGame.getTime()));
				break;
			case NB_CARDS:
				label.setText(mParentActivity.getString(R.string.menu_nbcards_title));
				desc.setText(mParentActivity.getString(R.string.menu_nbcards_summary));
				imgLeft.setImageResource(R.drawable.ic_menu_android);
				additionalText.setText(String.valueOf(mGame.getNbCards()));
				break;
			case USE_CUSTOM:
				label.setText("Utiliser les jeux personnalisés");
				desc.setText("Permet d'utiliser les personnages que vous avez créés");
				imgLeft.setImageResource(R.drawable.ic_menu_add);
				break;
			case CUSTOM:
				label.setText(mParentActivity.getString(R.string.menu_custom_title));
				desc.setText(mParentActivity.getString(R.string.menu_custom_summary));
				imgLeft.setImageResource(R.drawable.ic_menu_add);
				break;
			case RULES:
				label.setText(mParentActivity.getString(R.string.menu_rules_title));
				desc.setText(mParentActivity.getString(R.string.menu_rules_summary));
				imgLeft.setImageResource(R.drawable.ic_menu_gear);
				break;
			}
			v.setOnClickListener(new MenuClickListener(getContext(), menuItem));
		}
		return v;
	}

	class MenuClickListener implements View.OnClickListener {

		private final MenuItem mMenuClicked;

		public MenuClickListener(Context context, MenuItem menuClicked) {
			mMenuClicked = menuClicked;
		}

		@Override
		public void onClick(View v) {
			TextView additionalText = (TextView) v.findViewById(R.id.additionalText);
			switch (mMenuClicked.getType()) {
			case TEAMS:
				mGame.setNextTeam();
				additionalText.setText(String.valueOf(mGame.getNbTeams()));
				break;
			case TIME:
				mGame.setNextTime();
				additionalText.setText(String.valueOf(mGame.getTime()));
				break;
			case NB_CARDS:
				mGame.setNextNbCards();
				additionalText.setText(String.valueOf(mGame.getNbCards()));
				break;
			case USE_CUSTOM:
				mGame.setCustomCharacter(!mGame.isCustomCharacter());
				if (mGame.isCustomCharacter()) {
					mGame.getCharacters().clear();
					List<Character> charactersList = AppDbAPI.getInstance().getUserCharacters(mParentActivity.getApplicationContext());
					mGame.getCharacters().addAll(charactersList);
				} else {
					mGame.getCharacters().clear();
				}
				break;
			case CUSTOM:
				Intent intent = new Intent(mParentActivity.getApplicationContext(), CustomCharsActivity.class);
				if (intent != null) {
					mParentActivity.startActivity(intent);
				}
				break;
			case RULES:
				showRulesDialog();
				break;
			}
		}

		/** Show game rules dialog */
		private void showRulesDialog() {
			AlertDialog.Builder builder = new AlertDialog.Builder(mParentActivity);
			builder.setTitle(mParentActivity.getString(R.string.menu_rules_title));
			builder.setMessage(Html.fromHtml(mParentActivity.getString(R.string.rules_description)));
			builder.create().show();
		}
	}

}
